/*
 * @Description  :
 * @Author       : YiDing Xue
 * @Date         : 2023-02-22
 * @LastEditors  : YiDing Xue
 * @LastEditTime : 2023-03-13
 */
import { defineConfig, loadEnv } from 'vite'
import { createVitePlugins } from './build/plugin'
import { convertEnv, getSrcPath, getRootPath } from './build/utils'
import { createViteProxy } from './build/config'

export default defineConfig(({ command, mode }) => {
  const srcPath = getSrcPath()
  const rootPath = getRootPath()
  const isBuild = command === 'build'

  const env = loadEnv(mode, process.cwd())
  const viteEnv = convertEnv(env)
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_USE_PROXY, VITE_PROXY_TYPE } = viteEnv

  return {
    base: VITE_PUBLIC_PATH || '/',
    resolve: {
      alias: {
        '~': rootPath,
        '@': srcPath
      }
    },
    // define: viteDefine,
    plugins: createVitePlugins(viteEnv, isBuild),
    // 配置全局样式变量
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@nutui/nutui/dist/styles/variables.scss";`
        }
      }
    },
    server: {
      host: '0.0.0.0',
      port: VITE_PORT,
      open: true,
      proxy: createViteProxy(VITE_USE_PROXY, VITE_PROXY_TYPE)
    },
    build: {
      target: 'es2015',
      outDir: 'dist',
      reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告
      chunkSizeWarningLimit: 1024 // chunk 大小警告的限制（单位kb）
    }
  }
})
